﻿using BTechBids.Web.IDataAdapters.Adapters;
using BTechBids.Web.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BTechBids.Web.Singleton;
using BTechBids.DataModels;
using BTechBids.Web.Models;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;


namespace BTechBids.Web.Controllers
{
    [Authorize]
    public class ItemController : ApiController
    {
        IItemAdapter _adapter;
        Singleton.Singleton _instance;

        public ItemController()
        {
            _instance = Singleton.Singleton.Instance;
            _adapter = new ItemAdapter();
        }


        //GET api/item
        [AllowAnonymous]
        [ActionName("index")]
        public IHttpActionResult Get()
        {
            List<Models.ItemVM> items = _instance.GetActiveItems().OrderBy(x => x.msRemaining).ToList();
            return Ok(items);
        }

        // GET api/item/quick/
        [AllowAnonymous]
        [ActionName("quick")]
        public IHttpActionResult GetQuickUpdates()
        {
            List<ItemQuickUpdateVM> quickItems = _instance.GetQuickUpdateItems();
            return Ok(quickItems);
        }

        // GET api/item/quick/:id
        [AllowAnonymous]
        [ActionName("quick")]
        public IHttpActionResult GetQuickItem(int id)
        {
            ItemQuickUpdateVM itemQuick = _instance.GetQuickUpdateForItem(id);
            if (itemQuick == null)
            {
                ItemVM item = _adapter.GetItem(id);
                return Ok(item);
            }

            return Ok(itemQuick);
        }


        // GET api/item/detail/:id
        [AllowAnonymous]
        [ActionName("detail")]
        public IHttpActionResult GetDetail(int id)
        {
            ItemVM item = _instance.GetItemIfActive(id);
            if (item == null)
            {
                item = _adapter.GetItem(id);
            }

            return Ok(item);
        }

        //POST api/item/bid/:id
        [ActionName("bid")]
        public IHttpActionResult PostNewBid(int id)
        {
            string username = User.Identity.GetUserName();
            if (_adapter.GetUserBids(username) > 0)
            {
                ItemQuickUpdateVM item = _instance.MakeBid(id, username);
                return Ok(item);
            }
            return NotFound();
        }

        //POST api/item
        public IHttpActionResult Post([FromBody] ItemVM item)
        {
            if (User.IsInRole("Admin"))
            {
                item = _adapter.AddItem(item);
                _instance.AddNewItem(item);
                return Ok(item);
            }
            return Unauthorized();
        }

        //PUT api/item/:id
        public IHttpActionResult Put([FromBody] ItemVM itemvm)
        {
            if (User.IsInRole("Admin"))
            {
                itemvm = _adapter.UpdateItem(itemvm);
                return Ok(itemvm);
            } 
            return Unauthorized();
        }

        //DELETE api/item/delete/:id
        [ActionName("delete")]
        public IHttpActionResult DeleteItem(int id)
        {
            if (User.IsInRole("Admin"))
            {
                ItemVM itemvm = _instance.DeleteAuction(id);
                if (itemvm == null)
                {
                    return NotFound();
                }
                _adapter.DeleteItem(itemvm.Id);
                return Ok();
            }
            return Unauthorized();
        }

        //get bid history api/item/bidhist/:id
        [AllowAnonymous]
        [ActionName("bidhist")]
        public IHttpActionResult GetBidHistory(int id)
        {
            return Ok(_adapter.GetBidHistory(id));
        } 
    }
}
